using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using XProject.Entity;

namespace XProject.DAO
{
    class BookCategoryDAO:DBAccess
    {
        #region sql command
        string SQL_SELECT_ID = "select max(BookCategoryID) from BookCategory";
        string SQL_SELECT_ALL = "select * from BookCategory";
        string SQL_INSERT = "insert into BookCategory([CategoryName],[Label]) values (@name, @label)";
        string SQL_UPDATE = "update BookCategory set [CategoryName]=@name, [Label]=@label where BookCategoryID=@id";
        string SQL_DELETE = "delete from BookCategory where BookCategoryID=@id";
        #endregion
        #region field
        string message;
        #endregion
        #region properties
        public string Message
        {
            get { return message; }
        }
        #endregion
        #region constructor
        public BookCategoryDAO()
        {
        }
        #endregion
        #region methods
        public IList<BookCategory> GetAll()
        {
            IList<BookCategory> list = new List<BookCategory>();
            try
            {
                OleDbCommand command = new OleDbCommand(SQL_SELECT_ALL, conn);
                if (Connect())
                {
                    OleDbDataReader dr = command.ExecuteReader();

                    while (dr.Read())
                    {
                        BookCategory bc = new BookCategory();
                        bc.CategoryID = dr.GetInt32(0);
                        bc.CategoryName = dr.GetString(1);
                        bc.Label = dr.GetString(2);
                        list.Add(bc);
                    }
                    Disconnect();
                }
            }
            catch (OleDbException ex)
            {
                message = ex.Message;
            }
            catch (Exception ex) { message = ex.Message; }
            return list;
        }
        public int Insert(BookCategory bc)
        {
            int effectedRows = 0;
            OleDbCommand command = new OleDbCommand(SQL_INSERT,conn);            
            if (Connect())
            {
                command.Parameters.Add("@name", OleDbType.VarChar).Value = bc.CategoryName;
                command.Parameters.Add("@label", OleDbType.VarChar).Value = bc.Label;
                
                effectedRows = command.ExecuteNonQuery();
                command.CommandText = SQL_SELECT_ID;
                bc.CategoryID = int.Parse(command.ExecuteScalar().ToString());
                Disconnect();
            }
            return effectedRows;
        }
        public int Update(BookCategory bc)
        {
            int effectedRows = 0;
            OleDbCommand command = new OleDbCommand(SQL_UPDATE, conn);
            if (Connect())
            {
                command.Parameters.Add("@name", OleDbType.VarChar).Value = bc.CategoryName;
                command.Parameters.Add("@label", OleDbType.VarChar).Value = bc.Label;
                command.Parameters.Add("@id", OleDbType.Integer).Value = bc.CategoryID;
                effectedRows = command.ExecuteNonQuery();               
                Disconnect();
            }
            return effectedRows;
        }
        public int Delete(BookCategory bc)
        {
            int effectedRows = 0;
            OleDbCommand command = new OleDbCommand(SQL_DELETE, conn);
            if (Connect())
            {                
                command.Parameters.Add("@id", OleDbType.Integer).Value = bc.CategoryID;
                effectedRows = command.ExecuteNonQuery();
                Disconnect();
            }
            return effectedRows;
        }
        #endregion
    }
}
